package cn.iocoder.yudao.module.vos.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.module.vos.dal.dataobject.ecdr.ECdrDO;
import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;


@Slf4j
public class CdrUtil {

    public static final ZoneId ZONE = ZoneId.of("Asia/Manila");

    public static String md5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : messageDigest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            log.error(e.getMessage());
            return input;
        }
    }

    public static String trimNumber(String phoneNumber) {
        if (phoneNumber.length() <= 10) {
            return phoneNumber;
        } else {
            return phoneNumber.substring(phoneNumber.length() - 10);
        }
    }

    public static String computePhoneTag(ECdrDO row) {
        String gw = nvl(row.getCallergatewayid());
        String calleeAcc = nvl(row.getCalleeaccesse164());

        if (gw.contains("催收")) {
            return "2";
        }
        if (gw.contains("营销")) {
            return "1";
        }

        if ("XY收费".equals(gw)) {
            if (startsWithAny(calleeAcc, "22101802", "22101803")) {
                return "2";
            }
            if (calleeAcc.startsWith("22101801")) {
                return "1";
            }
        }
        return "3";
    }

    private static boolean startsWithAny(String s, String... prefixes) {
        if (s == null) {
            return false;
        }
        for (String p : prefixes) {
            if (p != null && s.startsWith(p)) {
                return true;
            }
        }
        return false;
    }

    private static String nvl(String s) {
        return s == null ? "" : s;
    }

    public static String dateConvert(Long timestamp) {
        ZonedDateTime zonedDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneId.of("Asia/Manila"));
        Date date = Date.from(zonedDateTime.toInstant());
        DateTime dateTime = new DateTime(date);
        String formatted = DateUtil.format(dateTime, "yyyy-MM-dd HH:mm:ss");
        return formatted;
    }

    /** 将 yyyymmdd 转为 yyyy-MM-dd */
    public static String toYmd(String day8) {
        if (day8 == null || day8.length() != 8) return day8;
        return day8.substring(0, 4) + "-" + day8.substring(4, 6) + "-" + day8.substring(6, 8);
    }

    // 通用取值工具：越界/空串/全空白 => null
    public static String getOrNull(String[] arr, int idx) {
        if (arr == null || idx < 0 || idx >= arr.length) return null;
        String v = arr[idx];
        if (v == null) return null;
        v = v.trim();
        return v.isEmpty() ? null : v;
    }
}
